﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
namespace lcs.Web.myplandmaterialinfo
{
    public partial class List : Web.Admin.BasePage
    {
        
        
        
		lcs.BLL.myplandmaterialinfo bll = new lcs.BLL.myplandmaterialinfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                BindData();
                Page.DataBind();
            }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int mpmid = (Convert.ToInt32(Request.Params["id"]));
                string txtkey = txtKeyword.Text.Trim();
                string strnum = txtnum.Text.Trim();
                decimal num = 0;
                decimal.TryParse(strnum, out num);
                int id = 0;
                if (txtkey.Contains(":"))
                {
                    string temp = txtkey.Split(':')[1];
                    int.TryParse(temp, out id);
                    if (id > 0)
                    {
                        //进行数据查找并插入
                        bll.InsertBylink(id, mpmid, num);
                    }
                }
                BindData();
            }
        }
        
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0) 
                return;
            bll.DeleteList(idlist);
            BindData();
        }

        #region gridView
        private DataSet GetData()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int mpmid = (Convert.ToInt32(Request.Params["id"]));
                hladd.NavigateUrl = "Add.aspx?id=" + mpmid;
                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat("mpid ={0}", mpmid.ToString());
                ds = bll.GetList(strWhere.ToString());
                return ds;
            }
            else
                return null;
        }
        public void BindData()
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int mpmid = (Convert.ToInt32(Request.Params["id"]));
                hladd.NavigateUrl = "Add.aspx?id=" + mpmid;
                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat("mpid ={0}", mpmid.ToString());
                ds = bll.GetList(strWhere.ToString());
                gridView.DataSource = ds;
                gridView.DataBind();
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
                
                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
               
            }
        }
        
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {                        
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion
        public void ExportExcel(System.Web.UI.WebControls.WebControl objControl, string strFileName)
        {

            strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "gb2312";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            objControl.RenderControl(oHtmlTextWriter);

            objControl = null;
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>" + oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.End();
        }
        protected void btn_out_Click(object sender, EventArgs e)
        {
            DataSet ds = GetData();
            gridView.DataSource = ds;

            gridView.AllowPaging = false;
            gridView.Columns[0].Visible = false;
            gridView.Columns[3].Visible = false;
            gridView.DataBind();
            string f = DateTime.Now.Ticks.ToString();
            ExportExcel(gridView, f);
            gridView.DataSource = ds;
            gridView.AllowPaging = true;
            gridView.Columns[0].Visible = true;
            gridView.Columns[3].Visible = true;
            gridView.DataBind();

           
        }

        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }



    }
}
